﻿using System;
using System.Threading;
using System.Windows;
using Relay.Core;
using Relay.Core.Plugins;
using Relay.Core.Protocol;
using Relay.UI.Controls;
using Relay.UI.Controls.Docking;

namespace Relay.UI {
    public class RelayUI : IInterfacePlugin {
        #region Fields
        private ApplicationService _appService = null;
        private Application _app = null;
        private MainWindow _mainWindow = null;
        #endregion

        #region Protected Methods
        /// <summary>
        /// Raises the <see cref="E:Error"/> event.
        /// </summary>
        /// <param name="e">The <see cref="Relay.Core.MessageEventArgs"/> instance containing the event data.</param>
        protected virtual void OnError(MessageEventArgs e) {
            if (Error != null) Error(this, e);
        }

        /// <summary>
        /// Raises the <see cref="E:Initialized"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnInitialized(EventArgs e) {
            if (Initialized != null) Initialized(this, e);
        }

        /// <summary>
        /// Raises the <see cref="E:MainWindowClosed"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnMainWindowClosed(EventArgs e) {
            if (MainWindowClosed != null) MainWindowClosed(this, e);
        }
        #endregion

        #region Private Methods
        private void WindowClosed(object sender, EventArgs e) {
            OnMainWindowClosed(new EventArgs());
        }
        #endregion

        #region IInterfacePlugin Members
        /// <summary>
        /// Occurs when the main window is closed.
        /// </summary>
        public event EventHandler<EventArgs> MainWindowClosed;

        /// <summary>
        /// Display this interface.
        /// </summary>
        public void DisplayInterface() {
            _mainWindow.Show();
        }

        #endregion

        #region IPlugin Members
        /// <summary>
        /// Occurs when an error occurs within the plugin.
        /// </summary>
        public event EventHandler<MessageEventArgs> Error;

        /// <summary>
        /// Occurs when the plugin has successfully initialized.
        /// </summary>
        public event EventHandler<EventArgs> Initialized;

        /// <summary>
        /// Perform any initialization that is necessary.
        /// </summary>
        /// <param name="host">The plug-in host that hosts this plug-in.</param>
        /// <returns></returns>
        public PluginLoadResult Initialize(ApplicationService host) {
            _appService = host;
            _app = new Application();
            _app.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                new ThreadStart(delegate { _app.Run(); }));

            _mainWindow = new MainWindow(host);
            _mainWindow.Closed += new EventHandler(WindowClosed);

            OnInitialized(new EventArgs());
            return new PluginLoadResult(true);
        }

        /// <summary>
        /// Terminate the plugin and clean up any resources that may be open.
        /// </summary>
        public void Terminate() {
            
        }

        /// <summary>
        /// Gets the name of the plugin.
        /// </summary>
        public string Name {
            get { return "RelayUI"; }
        }

        /// <summary>
        /// Gets the description of the plugin.
        /// </summary>
        public string Description {
            get { return "Default Relay UI"; }
        }

        /// <summary>
        /// Gets the author of the plugin.
        /// </summary>
        public string Author {
            get { return String.Empty; }
        }

        #endregion
    }
}
